package cn.cxyxj.study08;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @author cxyxj
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {




    @Bean
    PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername("cxyxj").password("123").roles("admin").build());
        manager.createUser(User.withUsername("security").password("security").roles("user").build());
        return manager;
    }

        @Override
        protected void configure(HttpSecurity http) throws Exception {

            http.authorizeRequests()  //开启配置
                    .antMatchers("/cxyxj/**").hasRole("admin")   //访问/cxyxj/**下的路径，必须具备admin身份
                    .antMatchers("/security/**").hasRole("user") //访问/security/**下的路径，必须具备user身份
                    .antMatchers("/permitAll").permitAll() // 访问/permitAll路径，不需要登录
                    .anyRequest() //其他请求
                    .authenticated()//验证   表示其他请求只需要登录就能访问
                    .and()
                    .formLogin(); // 开启表单登陆
        }





}
